import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.swing.*;

/** Stand alone application 
 * - open / save sketch files
 * 
 * @author manoj
 *
 */
public class Standalone
{
	public static void main (String argv [])
    { 
 		GUIThread guiThread = new GUIThread();
        java.awt.EventQueue.invokeLater(guiThread);        
    }
}

class GUIThread implements Runnable
{

    public void run() 
    {
        new DrawFrame();
    }
}


/**
 * Frame containing a menu bar 
 * @author manoj
 *
 */
class DrawFrame extends JFrame implements ActionListener
{
	MenuBar menuBar;
	Menu filemenu, drawingMenu, personaMenu, editMenu;
	Menu personaList, pressureSettings, tiltSettings, otherSettings, submenu;
	MenuItem menuItem;
	JFileChooser fc;
	
	JPanel panel;
	
	// Panel with controls
	JPanel controls;
	
	JLabel brush;
	JSlider brushSlider;
	
	
	DrawPanel p1;
	
	
		
	DrawFrame()
	{
		
		
		p1=new DrawPanel();
		p1.setPreferredSize(new Dimension(1500,800));
		p1.setBackground(Color.WHITE);
		
		// Creating the FileChooser Open/ Save Dialog box
		fc = new JFileChooser();
		
		panel = new JPanel();
		
		//Panel with Controls
		/*controls=new JPanel();
		brush= new JLabel("Brush");
		brushSlider = new JSlider(1, 10);
		controls.add(brush);
		controls.add(brushSlider);
				*/
		// Creating Menu Bar and adding File menu items
		menuBar = new MenuBar();
		
		//Building the file menu
		filemenu = new Menu("File");
		filemenu.setActionCommand("file");
		menuBar.add(filemenu);
		
		//Building the edit menu
		editMenu = new Menu("Edit");
		editMenu.setActionCommand("edit");
		menuBar.add(editMenu);
		
		//Building the drawing menu
		drawingMenu = new Menu("Drawing");
		drawingMenu.setActionCommand("drawing");
		menuBar.add(drawingMenu);
		
		//Building the Persona menu
		personaMenu = new Menu("Persona");
		personaMenu.setActionCommand("persona");
		menuBar.add(personaMenu);
		
		//Building the pressure settings submenu
		pressureSettings = new Menu("Pressure");
		pressureSettings.setActionCommand("pressureSetting");
		drawingMenu.add(pressureSettings);
		
		//Building the tilt settings submenu
		tiltSettings = new Menu("Tilt");
		tiltSettings.setActionCommand("activate");
		drawingMenu.add(tiltSettings);
		
		//Building the other settings submenu
		otherSettings = new Menu("Also");
		otherSettings.setActionCommand("other");
		drawingMenu.add(otherSettings);
		
		//Building the Persona List submenu
		personaList = new Menu("Activate Persona");
		personaList.setActionCommand("activate");
		personaMenu.add(personaList);
		
		//Adding a persona
		menuItem = new MenuItem("None");
		menuItem.setActionCommand("p0");
		menuItem.addActionListener(this);
		personaList.add(menuItem);
		
		//Adding a persona
		menuItem = new MenuItem("Persona 1");
		menuItem.setActionCommand("p1");
		menuItem.addActionListener(this);
		personaList.add(menuItem);
		
		//Adding a persona
		menuItem = new MenuItem("Persona 2");
		menuItem.setActionCommand("p2");
		menuItem.addActionListener(this);
		personaList.add(menuItem);
		
		//Adding a persona
		menuItem = new MenuItem("Persona 3");
		menuItem.setActionCommand("p3");
		menuItem.addActionListener(this);
		personaList.add(menuItem);
		
		//Adding a persona
		menuItem = new MenuItem("Persona 4");
		menuItem.setActionCommand("p4");
		menuItem.addActionListener(this);
		personaList.add(menuItem);
		
		//Adding a persona
		menuItem = new MenuItem("Persona 5");
		menuItem.setActionCommand("p5");
		menuItem.addActionListener(this);
		personaList.add(menuItem);
		
		//Adding clear canvas item
		menuItem = new MenuItem("Clear");
		menuItem.setActionCommand("clear");
		menuItem.addActionListener(this);
		editMenu.add(menuItem);
		
		//Adding Open menu item 
		menuItem = new MenuItem("Open");
		menuItem.setActionCommand("open");		
		menuItem.addActionListener(this);
		filemenu.add(menuItem);
		
		//Adding Save Menu item 
		menuItem = new MenuItem("Save");
		menuItem.setActionCommand("save");
		menuItem.addActionListener(this);
		filemenu.add(menuItem);
		
		//Adding Exit Menu Item
		menuItem = new MenuItem("Quit");
		menuItem.setActionCommand("exit");
		menuItem.addActionListener(this);
		filemenu.add(menuItem);
		
		//Adding start calibration
		menuItem = new MenuItem("Finish Calibration");
		menuItem.setActionCommand("fCal");
		menuItem.addActionListener(this);
		personaMenu.add(menuItem);
		
		//Adding pressure nothing
		menuItem = new MenuItem("None");
		menuItem.setActionCommand("pNone");
		menuItem.addActionListener(this);
		pressureSettings.add(menuItem);
		
		//Adding pressure controls thickness
		menuItem = new MenuItem("Thickness");
		menuItem.setActionCommand("pThick");
		menuItem.addActionListener(this);
		pressureSettings.add(menuItem);
		
		//Adding pressure controls darkness
		menuItem = new MenuItem("Weight");
		menuItem.setActionCommand("pDark");
		menuItem.addActionListener(this);
		pressureSettings.add(menuItem);
		
		//Adding tilt controls nothing
		menuItem = new MenuItem("None");
		menuItem.setActionCommand("tNone");
		menuItem.addActionListener(this);
		tiltSettings.add(menuItem);
		
		//Adding tilt controls thickness
		menuItem = new MenuItem("Thickness");
		menuItem.setActionCommand("tThick");
		menuItem.addActionListener(this);
		tiltSettings.add(menuItem);
		
		//Adding other nothing
		menuItem = new MenuItem("None");
		menuItem.setActionCommand("oNone");
		menuItem.addActionListener(this);
		otherSettings.add(menuItem);
		
		//Adding mystery colors
		menuItem = new MenuItem("Mystery Color");
		menuItem.setActionCommand("mColor");
		menuItem.addActionListener(this);
		otherSettings.add(menuItem);
		
		//Adding earth colors
		menuItem = new MenuItem("Earth Color");
		menuItem.setActionCommand("eColor");
		menuItem.addActionListener(this);
		otherSettings.add(menuItem);

		//getContentPane().setLayout(new GridLayout());
		
		
		this.setMenuBar(menuBar);
		//panel.add(controls);
		//panel.add(p1);
		getContentPane().add(p1);
		setSize(600,600);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}		

	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getActionCommand().equalsIgnoreCase("Open"))
		{
			// Open file dialog + loading the file into Sketch object
			int retval = fc.showOpenDialog(this);
			if(retval == JFileChooser.APPROVE_OPTION)
			{
				File f = fc.getSelectedFile();
				if(f.exists())
					p1.s.loadSketch(f);
			}
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("Modify"))
		{
                        // Modify sketches to persona

                        //Create object to draw
                        /*int size= p1.s.strokeList.size();
                        for(int i=0; i< size; i++)
                        {
                          for(int j=0; j<p1.s.strokerList.get(i).size();j++)
                          {
                          }
                        }
                        //add object to p1
                        p1.
                        p1.repaint();
			*/
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("Save"))
		{
			// Save file dialog + Saving the sketch object into a file 
			int retval = fc.showOpenDialog(this);
			if(retval == JFileChooser.APPROVE_OPTION)
			{
				File f = fc.getSelectedFile();
				
				try
				{
					
					FileWriter fw = new FileWriter(f);				
					String xml = p1.s.toXML();
					fw.write(xml);					
					fw.close();
					
				}catch(IOException e)
				{
				
				}
				
			}
			
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("Exit"))
		{
			System.exit(0);
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("pNone"))
		{
			p1.pressureClear();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("pThick"))
		{
			p1.pressureThickness();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("pDark"))
		{
			p1.pressureWeights();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("tNone"))
		{
			p1.tiltClear();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("tThick"))
		{
			p1.tiltThickness();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("oNone"))
		{
			p1.otherClear();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("mColor"))
		{
			p1.magicColors();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("eColor"))
		{
			p1.earthColors();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("sCal"))
		{
			p1.clearCanvas();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("fCal"))
		{
		    p1.createPersona();
			p1.clearCanvas();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("clear"))
		{
			p1.clearCanvas();
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("p0"))
		{
		    p1.activatePersona(0);
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("p1"))
		{
		    p1.activatePersona(1);
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("p2"))
		{
		    p1.activatePersona(2);
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("p3"))
		{
		    p1.activatePersona(3);
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("p4"))
		{
		    p1.activatePersona(4);
		}
		else if(arg0.getActionCommand().equalsIgnoreCase("p5"))
		{
		    p1.activatePersona(5);
		}
		else
		{
			System.out.println("Not a File Menu");
		}
	}
}
  	